﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace exe2exe
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();



        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide(); //先隐藏主窗体

            Form1 form1 = new Form1(); //重新实例化此窗体

            form1.ShowDialog();//已模式窗体的方法重新打开

            this.Close();
        }

        private static bool GetDotNetVersion(string version)
        {
            string oldname = "0";
            using (RegistryKey ndpKey =
                RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "").
                OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
            {
                foreach (string versionKeyName in ndpKey.GetSubKeyNames())
                {
                    if (versionKeyName.StartsWith("v"))
                    {
                        RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName);
                        string newname = (string)versionKey.GetValue("Version", "");
                        if (string.Compare(newname, oldname) > 0)
                        {
                            oldname = newname;
                        }
                        if (newname != "")
                        {
                            continue;
                        }
                        foreach (string subKeyName in versionKey.GetSubKeyNames())
                        {
                            RegistryKey subKey = versionKey.OpenSubKey(subKeyName);
                            newname = (string)subKey.GetValue("Version", "");
                            if (string.Compare(newname, oldname) > 0)
                            {
                                oldname = newname;
                            }
                        }
                    }
                }
            }
            return string.Compare(oldname, version) > 0 ? true : false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (!GetDotNetVersion("4.8"))
            {
                label3.Text = "未通过";
                //if (MessageBox.Show("当前缺少运行环境，是否进行安装！\r\n\r\n安装完成后将自动启动软件", "提示", MessageBoxButtons.YesNo) == DialogResult.No)
                    //return;
                //Process.Start(@"_framework4.5.2.exe").WaitForExit(); //一直等待，直到Framework安装完成
                if (GetDotNetVersion("8"))
                    //Process.Start(@"你的程序.exe");
                    //MessageBox.Show("环境8");
                    label3.Text = "通过";
            }
            else
                //Process.Start(@"你的程序.exe");
                //MessageBox.Show("环境8");
                label3.Text = "通过";


            
            using (var key = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(@"Local Settings\Software\Microsoft\Windows\CurrentVersion\AppModel\PackageRepository\Packages\"))
            {
                if (key != null)
                {
                    foreach (var subkey in key.GetSubKeyNames())
                    {
                        if (subkey.StartsWith("Microsoft.MicrosoftEdge"))
                        {
                            label5.Text = "通过";
                            break;
                        }
                        else
                            label5.Text = "未通过";
                    }
                }
            }

            if (label3.Text == "未通过" )
            {
                button1.Visible = true;
                button2.Visible = true;
            }

            if (label5.Text == "未通过")
            {
                button1.Visible = true;
                button3.Visible = true;
            }

            if (label3.Text == "通过"&& label5.Text == "通过")
            {
                process3.Start();
                this.Close();
            }

            label7.Text = Environment.OSVersion.VersionString;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            process1.Start();
        }
    }   

    
}
